# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=grommunio-gromox
subpackages="$pkgname-dbg $pkgname-doc $pkgname-dev $pkgname-openrc"
pkgver=2.27
pkgrel=0
pkgdesc="Open Source Groupware Solution"
arch="all !riscv64"
url="https://grommunio.com/"
license="AGPL-3.0-or-later"
_php=php83
install="$pkgname.pre-install"
depends="
	grommunio-common
	mariadb
	$_php
	$_php-fpm
	postfix
	redis
	!kopano-core
"

makedepends="
	autoconf
	automake
	curl-dev
	gumbo-parser-dev
	jsoncpp-dev
	libhx-dev
	libtool
	libvmime-dev
	libxml2-dev
	linux-pam-dev
	linux-headers
	mariadb-dev
	musl-dev
	openldap-dev
	openssl-dev
	$_php
	$_php-dev
	sqlite-dev
	tinyxml2-dev
	zlib-dev
	zstd-dev
"

pkgusers="grommunio gromox"
pkggroups="grommunio gromox gromoxcf"

source="
	https://github.com/grommunio/gromox/archive/refs/tags/gromox-$pkgver.tar.gz

	gromox-delivery-queue.initd
	gromox-delivery.initd
	gromox-event.initd
	gromox-http.initd
	gromox-imap.initd
	gromox-midb.initd
	gromox-pop3.initd
	gromox-timer.initd
	gromox-zcore.initd
	gromox-fpm.conf
	http.cfg
	imap.cfg
	mysql_adaptor.cfg
	pop3.cfg
	smtp.cfg
	zcore.cfg
	autodiscover.ini
"

builddir="$srcdir/gromox-gromox-$pkgver/"

prepare() {
	default_prepare
	autoreconf -fiv
}

build() {
	export CXXFLAGS="$CXXFLAGS -fpermissive"
	./configure --prefix=/usr

	make
}

check() {
	make check
}

package() {
	mkdir -p "$pkgdir"
	make install DESTDIR="$(realpath $pkgdir)"

	# prepare services
	for item in delivery-queue delivery event http imap midb pop3 timer zcore; do
		install -Dm 0755 "$srcdir/gromox-$item.initd" "$pkgdir/etc/init.d/$item"
	done

	# prepare configuration. some files are predefined, others are generated
	x500_org_name=$(awk 'BEGIN { srand(); print int(rand()*32768) }' /dev/null | md5sum | head -c 10)
	#sed -i.bak "s/RANDOM/$(echo $RANDOM | md5sum | head -c 10)/" "$srcdir/zcore.cfg"
	sed -i.bak "s/<x500_org_name>/$x500_org_name/" "$srcdir/zcore.cfg"
	sed -i.bak "s/<x500_org_name>/$x500_org_name/" "$srcdir/autodiscover.ini"
	install -dm 0750 "$srcdir/gromox"
	install -Dm 0640 "$srcdir/zcore.cfg" "$pkgdir/etc/gromox/zcore.cfg"
	grep x500_org_name > "$pkgdir/etc/gromox/exchange_emsmdb.cfg" < "$srcdir/zcore.cfg"
	grep x500_org_name > "$pkgdir/etc/gromox/exchange_nsp.cfg" < "$srcdir/zcore.cfg"
	grep x500_org_name > "$pkgdir/etc/gromox/exmdb_local.cfg" < "$srcdir/zcore.cfg"
	grep x500_org_name > "$pkgdir/etc/gromox/exmdb_provider.cfg" < "$srcdir/zcore.cfg"
	grep default_dom > "$pkgdir/etc/gromox/delivery.cfg" < "$srcdir/zcore.cfg"
	install -Dm 0640 "$srcdir/zcore.cfg" "$pkgdir/etc/gromox/midb.cfg"


	for item in http imap mysql_adaptor pop3 smtp; do
		install -Dm 0640 "$srcdir/$item.cfg" "$pkgdir/etc/gromox/$item.cfg"
	done

	# autodiscover.ini
	install -Dm 0640 "$srcdir/autodiscover.ini" "$pkgdir/etc/gromox/autodiscover.ini"

	# php-fpm
	install -Dm 0644 "$srcdir/gromox-fpm.conf" "$pkgdir/etc/$_php/php-fpm.d/gromox.conf"
	rm -f "$pkgdir/usr/share/gromox/fpm-gromox.conf.sample"

	# license
	install -Dm 0644 LICENSE.txt "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

	# create database directories
	for i in /var/lib/gromox \
		/var/lib/gromox/domain \
		/var/lib/gromox/user \
		/var/lib/gromox/queue \
		/var/lib/gromox/queue/cache \
		/var/lib/gromox/queue/mess \
		/var/lib/gromox/queue/save \
		/var/lib/gromox/queue/timer; do
		install -dm 0770 -g gromox -o gromox "$pkgdir/$i"
	done

	# make sure all files in /etc/gromox are owned by grommunio:grmoxcf
	chown -R grommunio:gromoxcf "$pkgdir/etc/gromox"
	find  "$pkgdir/etc/gromox" -type f -exec chmod 0640 {} ';'
	find  "$pkgdir/etc/gromox" -type d -exec chmod 0750 {} ';'

	# remove unnecessary folders
	rm -Rf "$pkgdir/usr/lib/systemd" "$pkgdir/usr/lib/sysusers.d/" "$pkgdir/usr/lib/tmpfiles.d/"

}

sha512sums="
98dcfc16f64a440a1efda8501f4d72636e7ab570946f5f573895c347a9f451573ac2b55c9032ad6b06e9e29a123e0dbe386a96556f0448ef7b8ac36ce28f2f18  gromox-2.27.tar.gz
444ddd5ff60eff1bbfeaccc96aaa98aaf7c2d0122d5fec3f9faa3339ebd90e423718113853787c57aea8354fa83e68e455938e5a1294d35445b06d54074f2e60  gromox-delivery-queue.initd
afdf55901ac406ccb8ddb87a337cf87af9b67f5fb850dd66ee58b12ee817c7ecd6bd25e349d70fd53940e7c79516fe7dde473c738f1c834b3a558988bf0a7e8a  gromox-delivery.initd
ee95b1f8397a270f434ae343fbb2187915b658198a09749b794e1811ca697f6cf2465e8b58ad0ba9ea3d8b14b7cc21355d0123845f6f9fecd751957a4bab2cfe  gromox-event.initd
3be4aba7806c37da9618e5b9abefed9aad15ec752ae7ff306e998a4a445efcfd89d4b6498924104feb915258e5a1869171f3420a71dad0f1d913ad1d071f603d  gromox-http.initd
390723a599e9805bf9de5f6dfe61ef9c9d2e37389ecf08869eee4a8192d3b4f3848e9b847a75e55fb9022f577fad43db7993c5430d4f8c81f39658d2093a4a13  gromox-imap.initd
cd1b1e9c14485d9fa2c4ac1db3095e5a29cc210324b190a68ff51899e8a92f27d7caa0cb4aa8b9cbb2e355aff95bf47f1e7b3811deff316245c07222168badc5  gromox-midb.initd
db91aa7e775a2b8c8756b48c1a7c2b3b2241b3b69d13486584b42c24da92420b65571b0d6b3fdb95dac6cc95bf9f2dffb894357511d084c5bff5bf2a5f74975e  gromox-pop3.initd
011d24cd10f113646cf0f13cc65198b0df862e59908e1f4dcd931d410954ab817b48fe818c6fe845d5c62228d6373380ba4afa8aefc85c57886004b84df0be76  gromox-timer.initd
cb3cf8b1d155ebdfdf948ff2104838b17ff8d915ac5bd2945a6fca2bba1b8a76fd54345cc865ea86a957f3d11e1b32c0b5c0f4f69ab4b1a3bac9116e616b060e  gromox-zcore.initd
adb0da75223284a1081166597ef42d267d2187f663b7da7c66eae517e0af179217464a1e7ec6b2d082f7dd0b506e1e980724cf1fec081719b253e8ea44a37558  gromox-fpm.conf
768b63b8d6431b47546753481e67cbd27797e8813dce4ba5109b511ee751972b9b7a137d0c933f29156e5d5b1f1d9b3d1c4733d522be6141b1b56b0b7a90b2e0  http.cfg
f6bfdf6e44ed1cb7718a71fc75920d0a92b22fd17166d85c43273b53c57931344aaa1be0b61b713f2f7d586ea621c1077b6585a4fc553198e49069db8e6a0396  imap.cfg
a4b54f32a4f8b932ea93dd71c1eaccac50a445d960f3ab37913481db3b80daad51238f4b826331111e939f5a75d522619fff9566042670b578c65c477754bd64  mysql_adaptor.cfg
1612fb72fa8417ef54670f82e829c1307256ab87b8ee478d9bddf32599888de407749ee4cd97c52e55dc72138964db531af061d9c8426d6dacb692a48b4bfbef  pop3.cfg
04e234844466beff40a67d8b0f1eeea93ca1d801c88127ccb4f4475665c16c3b3cc11077e32fc34e010b63af2557e9b3a214213ee5382b3c1ffa4c3ae131dc4a  smtp.cfg
b7f86af6b5a7f0d075c4c46c33c513db41ebd59a59a6e53613c2f1fcb09a03080eaede57e6040d609892d5c910dbbe03de3bf70035ecd8ed63f3161acb3425ab  zcore.cfg
1323813c698159340436c7023309abc26bef3dbfa17b6ea331f172cd26c6d7eb207fcfe4a8b4ff88042f7895ea49c6d09cfa247cb6586d97928b211d91528e24  autodiscover.ini
"
